﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Synchros
{
   public class LogHelper
    {
        private static readonly string WorkPath = System.Windows.Forms.Application.StartupPath;
        public static readonly string LogDirPath = WorkPath + "\\log";

        private static readonly int iMaxLineCount = 1000;

        public static void AddLog(string strContent)
        {
            AddLog(GetPath(), strContent);
        }
        //ggz:日志路径
        public static string GetPath()
        {
            return GetPath(DateTime.Now);
        }
        public static string GetPath(DateTime date)
        { 
            return LogHelper.LogDirPath + "\\Synchros" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
        }
        public static void AddLog(string fileName, string Content)
        {
            try            
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(LogDirPath);
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }
                if (!File.Exists(fileName))
                {
                    StreamWriter tmpSW = File.CreateText(fileName);
                    tmpSW.Close();
                }
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    int lenght = Content.Length;
                    int count = lenght / iMaxLineCount;
                    if (count == 0)
                    {
                        sw.WriteLine(Content);
                    }
                    else
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sw.WriteLine(Content.Substring(0, iMaxLineCount));
                    }
                    for (int i = 1; i < count - 1; i++)
                    {
                        sw.WriteLine(Content.Substring((iMaxLineCount * i), iMaxLineCount));
                    }

                    if (count > 0)
                    {
                        sw.WriteLine(Content.Substring(iMaxLineCount * count));
                    }
                    sw.Close();
                }
            }
            catch
            {
                ;
            }
        }
    }
}
